package com.inxedu.os.edu.service.article.impl;

import com.inxedu.os.edu.dao.EduArticleContentMapper;
import com.inxedu.os.edu.dao.EduArticleMapper;
import com.inxedu.os.edu.pojo.EduArticle;
import com.inxedu.os.edu.pojo.EduArticleContent;
import com.inxedu.os.edu.pojo.QueryArticle;
import com.inxedu.os.edu.service.article.AdminArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AdminArticleServiceImpl implements AdminArticleService {
    @Autowired
    private EduArticleMapper eduArticleMapper;
    @Autowired
    private EduArticleContentMapper eduArticleContentMapper;

    @Override
    public List<EduArticle> lookArticle(QueryArticle record) {
        return eduArticleMapper.lookArticleList(record);
    }

    @Override
    public void updateArticle(EduArticle eduArticle) {
        eduArticleMapper.updateArticle(eduArticle);
    }

    @Override
    public void updateArticleContent(EduArticleContent eduArticleContent) {
        eduArticleMapper.updateArticleContent(eduArticleContent);
    }

    @Override
    public EduArticle findArticleById(int articleId) {

        return eduArticleMapper.findArticleById(articleId);
    }

    @Override
    public String findArticleContentByArticleId(int articleId) {
        return eduArticleMapper.findArticleContentByArticleId(articleId);
    }

    @Override
    public void deleteArticleByIds(String[] articleIds) {
        if(articleIds != null || articleIds.length > 0){
            String ids="";
            for (int i = 0;i<articleIds.length;i++){
                if (i < articleIds.length - 1) {
                    ids += articleIds[i] + ",";
                } else {
                    ids += articleIds[i];
                }
            }
            eduArticleMapper.deleteArticleByIds(ids);
            eduArticleMapper.deleteArticleContentByArticleIds(ids);
        }
    }

    @Override
    public int addArticle(EduArticle article) {

        return eduArticleMapper.addArticle(article);
    }

    @Override
    public void addArticeContent(EduArticleContent articleContent) {

        eduArticleContentMapper.insert(articleContent);
    }
}
